<?php

namespace tool;

class Curl
{
    public static function curl($url='', $data=[],$header=[])
    {
        if(!$url){
            return '地址必须填写';
        }
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        if (!empty($data)) {
            $params[CURLOPT_POST] = true;
            $params[CURLOPT_POSTFIELDS] = \json_encode($data);
        }

        if (!empty($header)) {
            $newHeader = [];
            foreach ($header as $key => $value) {
                $newHeader[] = $key . ':' . $value;
            }
            $params[CURLOPT_HTTPHEADER] = $newHeader;
        }

        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        if(json_decode($content, true)){
            return json_decode($content, true);
        }
        return $content;
    }
}